package operator.filter;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import util.Printer;
import util.RxJavaHelper;
import util.ThreadHelper;

class TakeOperator {

    public static void main(String[] args) {
//        take1();
//        take2();
//        takeLast1();
        takeLast2();
    }

    /**
     * 只取前两项
     */
    static void take1() {
        Observable.just(1, 2, 3, 4, 5)
                .take(2)
                .subscribe(RxJavaHelper.getConsumer());
    }

    /**
     * 只取指定时间内上游产生的数据
     */
    static void take2() {
        Observable.interval(1, TimeUnit.SECONDS)
                .take(10, TimeUnit.SECONDS)
                .subscribe(RxJavaHelper.getConsumer());
        ThreadHelper.sleep();
    }


    /**
     * 只取后两项
     */
    static void takeLast1() {
        Observable.just(1, 2, 3, 4, 5)
                .takeLast(2)
                .subscribe(RxJavaHelper.getConsumer());
    }

    static void takeLast2() {
        Observable.interval(1, TimeUnit.SECONDS)
                .doOnNext(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        Printer.print("------------------------doOnNext aLong = " + aLong);
                    }
                })
                .take(10)
                .takeLast(2, TimeUnit.SECONDS)
                .subscribe(RxJavaHelper.getConsumer());
        ThreadHelper.sleep();
    }
}
